package com.ismarkendenis.crc;

public class CRCCollisionDetector {

	/**
	 * @param args
	 */
	public CRCCollisionDetector() {

	}

	public void detectCollision(int data, int bitstreamResult, int divisor) {
		int dataBitLength = 32 - Integer.numberOfLeadingZeros(data);
		int divisorBitLength = 32 - Integer.numberOfLeadingZeros(divisor);

		for (int i = 0; i < dataBitLength;) {
			int tempData = data;
			while (32 - Integer.numberOfLeadingZeros(tempData) > divisorBitLength) {
				tempData >>= 1;
			}
		}
	}

	public boolean[] crcBitStreamResult(boolean[] bitstream, boolean[] divisor) {
		int startIndex = 0;
		boolean bitstreamResultEnd = false;
		while (!bitstreamResultEnd) {
			boolean startbitfound = false;
			for (int i = startIndex; i < bitstream.length && !startbitfound; i++) {
				if (bitstream[i] == true) {
					startbitfound = true;
					startIndex = i;
				}
			}
			if ((!startbitfound)
					|| startIndex + divisor.length > bitstream.length) {
				bitstreamResultEnd = true;
			} else {
				for (int j = 0; j < divisor.length; j++) {
					bitstream[j + startIndex] ^= divisor[j];
				}
			}
		}
		return bitstream;
	}
}
